/**
 * Created by Julia on 2016/05/03.
 */
define(['jquery', 'domReady!', 'appLocalStorage', 'appConst', 'model/global', 'business/user/userConfig', 'appMessageBox', 'jquery-form/jquery-form.min'], function ($, domReady, appLocalStorage, appConst, global, userConfig, msgBox) {
    return {
        initCreateUser: function () {
            var search_key = appConst.searchKey.search_user_key + global.getUserId();
            $("#user-form").validate({
                rules:userConfig.rules,
                messages:userConfig.messages,
                // Ajax form submition
                submitHandler: function (form) {
                    $(form).ajaxSubmit({
                        success: function (d) {
                            if(d.code == 0) {
                                if (d.message == "EXCEPTION_GLOBAL_OBJECT_ALREADY_EXISTS") {
                                    msgBox.errorBox(d.message, $("#loginName").val());
                                } else {
                                    msgBox.errorBox(d.message, 'MENU_SYSTEM_USER');
                                }
                                return false;
                            }
                            $("#user-form").addClass('submited');
                            var searchparam = {
                                'search_LIKE_loginName': $('#loginName').val()
                            };
                            appLocalStorage.setLocal(search_key, JSON.stringify(searchparam));
                        },
                        error: function(e){
                            if (e.status == 401) {
                                msgBox.sessionFailureBox();
                            }
                            if (e.status == 500) {
                                msgBox.serverFailureBox();
                            }
                            if (e.status == 404) {
                                msgBox.pathFailureBox();
                            }
                        }
                    });
                },
                // Do not change code below
                errorPlacement: function (error, element) {
                    error.insertAfter(element.parent());
                }
            });
        },
        initEvent: function () {
            $('.u-status').click(function (e) {
                $('.u-status').each(function () {
                    $(this).removeClass($(this).attr('data-status'));
                });
                $(this).addClass($(this).attr('data-status'));
                $('#status').val($(this).attr('data-value'))
            });
            $('.u-status').first().click();

            $('#lk_addnew').click(function (e) {
                $('#user-form').removeClass('submited');
                $('#user-form')[0].reset();
                e.preventDefault();
            });
            //$.formbar();
        }
    }
});

